﻿<style src="../../style/statistics.css" scoped></style>
<template>
  <div class="containerTable">
    <!--表5蔬菜生产经营实体表T_VegProsecution-->
    <Row class="divSearch">
            <Col span="24" class="col-margin">
            <!--根据需求删除不不要查询条件-->
            所属父级菜单：
            <Select v-model="searchModel.ParentId" placeholder="全部" style="width: 100px" @on-change="getPageData">
                <Option value="">全部</Option>
                <Option :value='item.Id' v-for="item in ParentList" :key="item.Id">{{item.MenuName}}</Option>
            </Select>
            状态：
            <Select v-model="searchModel.Status" placeholder="全部" style="width: 100px" @on-change="getPageData">
                <Option value="">全部</Option>
                <Option value="1">启用</Option>
                <Option value="0">停用</Option>
            </Select>
            菜单名称：
            <Input v-model="searchModel.MenuName" placeholder="请输入查询的名称值..." style="width: 100px"></Input>
            <Button icon="ios-search" type="ghost" @click="getPageData()">查询</Button>
            <Button icon="plus" type="primary" @click="showType = true" class="searchButtonRight">新增</Button>
            <!--<Button icon="android-done-all" type="primary" @click="multiSelect">批量删除</Button>-->
            </Col>
        </Row>
        <Row>
            <Col span="24" class="col-margin">
            <Table size="small" :height="clientHeight" border :context="ctx" :columns="columnsList" :data="datas.rows" no-data-text='暂无数据...'></Table>
            </Col>
        </Row>
        <Row>
            <Col span="24">
            <Page class="page-style" :total="datas.total" placement="top" @on-change="getPageNumber" :page-size="searchModel.PageSize" :page-size-opts='pageSizeOpts' show-sizer @on-page-size-change="getPageSize" show-total show-elevator></Page>
            </Col>
        </Row>
        <Modal v-model="showType" :mask-closable="false" :closable="false">
            <p slot="header" class="title">
                <Icon type="ios-paper" size="20"></Icon>
                <span style="font-size: 20px">信息编辑</span>
            </p>
            <p slot="close">
                <Icon type="close-circled" color="red" size=25></Icon>
            </p>
            <div slot="footer">
                <Button type="primary" @click="save('dataModel')">提交</Button>
                <Button type="ghost" @click="cancel('dataModel')" style="margin-left: 8px">取消</Button>
            </div>
            <Form ref="dataModel" :model="dataModel" :rules="ruleValidate" :label-width="100">
                <Form-item label="名称" prop="MenuName">
                    <Input v-model="dataModel.MenuName" placeholder="请输入名称"></Input>
                </Form-item>
                <Form-item style="display:none;" prop="Id">
                    <Input v-model="dataModel.Id"></Input>
                </Form-item>
                <Form-item label="所属父级菜单" prop="ParentId">
                    <Cascader :data="MenuList" v-model="dataModel.Menus" change-on-select @on-change='cascaderChange'></Cascader>
                    <Input style="display:none;" v-model="dataModel.ParentId"></Input>
                </Form-item>
                <Form-item label="地址" prop="MenuUrl">
                    <Input v-model="dataModel.MenuUrl" placeholder="请输入地址"></Input>
                </Form-item>
                <Form-item label="图标" prop="MenuIcon">
                    <Input v-model="dataModel.MenuIcon" placeholder="请输入图标"></Input>
                </Form-item>
                <Form-item label="状态" prop="Status">
                    <Select v-model="dataModel.Status" placeholder="请选择状态">
                        <Option value="1">启用</Option>
                        <Option value="0">停用</Option>
                    </Select>
                </Form-item>
                <Form-item label="显示状态" prop="Status">
                    <Select v-model="dataModel.ShowType" placeholder="请选择状态">
                        <Option value="1">显示</Option>
                        <Option value="2">隐藏</Option>
                    </Select>
                </Form-item>
                <Form-item label="排序" prop="Sort">
                    <Input v-model="dataModel.Sort" placeholder="请输入排序"></Input>
                </Form-item>
                <Form-item label="备注" prop="Remark">
                    <Input v-model="dataModel.Remark" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="备注长度请小于150个字符..."></Input>
                </Form-item>
            </Form>
        </Modal>
        <Modal v-model="actionModalType" :mask-closable="false" :closable="false" width="600">
            <p slot="header" class="title">
                <Icon type="ios-paper" size="20"></Icon>
                <span style="font-size: 20px">操作功能列表</span>
            </p>
            <div slot="footer">
                <Button type="primary" @click="actionSave()">提交</Button>
                <Button type="ghost" @click="actionCancel()" style="margin-left: 8px">取消</Button>
            </div>
            <Row style="padding:2px 0 0 15px;">
                <Col>
                <Button icon="plus" type="ghost" size="small" @click="addFastAction">快速新增</Button>
                <Button icon="plus" type="ghost" size="small" @click="addAction">新增</Button>
                </Col>
            </Row>
            <Row style="padding:2px 0px 5px 15px;">
                <Col>
                <table class="gridtable">
                    <thead>
                        <th>操作地址</th>
                        <th>操作名称</th>
                        <th>代码</th>
                        <th>排序</th>
                        <th>状态</th>
                        <th>操作</th>
                    </thead>
                    <tbody>
                        <tr v-for="item in MActions">
                            <td style="width:30%;">
                                <Input v-model="item.CtrlUrl"></Input>
                            </td>
                            <td style="width:15%;">
                                <Input v-model="item.ActionName"></Input>
                            </td>
                            <td style="width:20%;">
                                <Input v-model="item.ActionCode"></Input>
                            </td>
                            <td style="width:10%;">
                                <Input v-model="item.Sort"></Input>
                            </td>
                            <td style="width:15%;">
                                <Select v-model="item.Status">
                                    <Option value="1">启用</Option>
                                    <Option value="0">停用</Option>
                                </Select>
                            </td>
                            <td style="text-align:center;width:10%;">
                                <Button type="ghost" size="small" icon="ios-trash-outline" style="color:#FF0000" @click="delOp(item)">
                                </Button>
                            </td>
                        </tr>
                    </tbody>
                </table>
                </Col>
            </Row>
        </Modal>
    </div>
</template>
<script>
export default {
    data() {
        return {
            MenuId: '',//操作列表的菜单编号
            MenuUrl: '',
            MActions: [],//操作列表
            actionModalType: false,//操作列表框弹出状态
            showType: false,//编辑框弹出状态
            ruleValidate: {//验证模型
                // ParentId: [{ required: false, message: '请选择所属父级菜单', trigger: 'blur' }],
                MenuName: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
                MenuUrl: [{ required: true, message: '地址不能为空', trigger: 'blur' }],
                MenuIcon: [{ required: true, message: '图标不能为空', trigger: 'blur' }],
                Status: [{ required: false, message: '状态不能为空', trigger: 'blur' }],
                Sort: [{ required: false, message: '排序不能为空', trigger: 'blur' }],
                Remark: [
                    { required: false, message: '备注长度请小于100个字符', trigger: 'blur' },
                    { type: 'string', max: 100, message: '备注长度请小于150个字符', trigger: 'blur' }
                ]
            },
            dataModel: {//编辑模型
                Id: 0,
                ParentId: '',
                MenuName: '',
                MenuUrl: '',
                MenuIcon: '',
                Status: '1',
                Sort: '1',
                Remark: '',
                Menus:[]
            },
            //查询模型：根据需求删除不不要查询条件
            searchModel: {
                ParentId: '',
                MenuName: '',
                Status: '',
                PageNumber: 1, PageSize: 20
            },
            MenuList: [],
            ParentList: [],
            pageSizeOpts: [20, 40, 50],//默认分页最大设置
            datas: {},//table数据对象
            dataIds: [],
            ctx: this,//table数据上下文
            columnsList: [//表头对象
                { title: "所属父级菜单", key: "ParentName", width: 200 },
                {
                    title: "名称", key: "MenuName", width: 250,
                    render: (h, params) => {
                        return h('div', [
                            h('Icon', {
                                props: {
                                    type: params.row.MenuIcon
                                }
                            }, params.row.MenuName),
                            h('span', params.row.MenuName),
                        ]);
                    }
                },
                { title: "地址", key: "MenuUrl", width: 200 },
                { title: "状态", key: "StatusName", width: 80 },
                { title: "显示状态", key: "ShowTypeName", width: 100 },
                { title: "排序", key: "Sort", width: 80 },
                { title: "备注", key: "Remark" },
                {
                    title: "操作", key: "Id", width: 250, align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                class: {
                                    ieditColor: true
                                },
                                props: {
                                    size: 'small',
                                    type: 'ghost',
                                    icon: 'pull-request'
                                }, style: {
                                    marginRight: '5px'
                                }, on: {
                                    click: () => {
                                        this.actionList(params.row.Id, params.row.MenuUrl);
                                    }
                                }
                            }, '操作管理'),
                            h('Button', {
                                class: {
                                    ieditColor: true
                                },
                                props: {
                                    size: 'small',
                                    type: 'ghost',
                                    icon: 'ios-compose'
                                }, style: {
                                    marginRight: '5px'
                                }, on: {
                                    click: () => {
                                        this.editInfo(params.row.Id)
                                    }
                                }
                            }, '编辑'),
                            h('Button', {
                                class: {
                                    ideleteColor: true
                                },
                                props: {
                                    size: 'small',
                                    type: 'ghost',
                                    icon: 'ios-trash-outline'
                                }, style: {
                                    marginRight: '5px'
                                }, on: {
                                    click: () => {
                                        this.delInfo(params.row.Id)
                                    }
                                }
                            }, '删除'),
                        ]);
                    }
                }],
            clientHeight: window.innerHeight - 260,//表格默认高度
        }
    },
    mounted() {
        //table高度计算方法
        const that = this;
        window.onresize = () => {
            return (() => {
                var griddiv = document.getElementById('griddiv');
                that.clientHeight = window.innerHeight - 255;
                griddiv.style.height = that.clientHeight + 'px';
            })()
        };
    },
    methods: {
        //行政区划级联选择
        cascaderChange(value, selectedData) {
            const index = value.length - 1;
            this.dataModel.ParentId = value[index];
        },
        initMenu() {
            var self = this;
            this.$http.get('Menu/GetCasTreeList').then(function (res) {
                switch (res.data.status) {
                    case 0:
                        self.$Modal.info({ title: "提示信息", content: res.data.message, desc: false });
                        break;
                    case 1:
                        self.MenuList = res.data.model;
                        break;
                    case -1:
                        self.$Modal.warning({ title: "提示信息", content: "修改失败,请与管理员联系!" });
                        break;
                }
            });
        },
        actionSave() {
            var self = this;
            var model = new Object();
            model.MenuId = this.MenuId;
            model.ActionList = this.MActions;
            self.$http.post('MenuAction', model).then(function (res) {
                if (res.data.status === 1) {
                    self.$Notice.success({ title: '保存成功！', desc: false });
                    self.actionModalType = false;
                }
                else {
                    self.$Modal.error({ title: '提示信息', content: res.data.message });
                }
            });
        },
        actionCancel() {
            this.actionModalType = false;
            this.MActions = [];
        },
        //操作管理
        actionList(id, url) {
            this.actionModalType = true;
            this.MenuId = id;
            this.MenuUrl = url;
            var self = this;
            self.$http.get('MenuAction', { params: { id: id } }).then(function (res) {
                if (res.data.status === 1) {
                    self.MActions = res.data.model;
                }
                else {
                    self.$Modal.error({ title: '提示信息', content: res.data.message });
                }
            });
        },
        addFastAction() {
            this.MActions = [{ "CtrlUrl": this.MenuUrl, "ActionCode": "add", "ActionName": "添加", "Sort": "1", "Status": "1" },
            { "CtrlUrl": this.MenuUrl, "ActionCode": "edit", "ActionName": "修改", "Sort": "2", "Status": "1" },
            { "CtrlUrl": this.MenuUrl, "ActionCode": "delete", "ActionName": "删除", "Sort": "3", "Status": "1" },
            { "CtrlUrl": this.MenuUrl, "ActionCode": "query", "ActionName": "查询", "Sort": "4", "Status": "1" },
            { "CtrlUrl": this.MenuUrl, "ActionCode": "save", "ActionName": "保存", "Sort": "5", "Status": "1" }
            ];
        },
        addAction() {
            var item = { "CtrlUrl": this.MenuUrl, "ActionCode": "", "ActionName": "", "Sort": this.MActions.length + 1, "Status": "1" };
            this.MActions.push(item);
        },
        delOp(m) {
            for (var i = 0; i < this.MActions.length; i++) {
                if (this.MActions[i].ActionCode == m.ActionCode && this.MActions[i].ActionName == m.ActionName) {
                    this.MActions.splice(i, 1);
                }
            }
        },
        //获取当前页码
        getPageNumber(pageIndex) {
            this.searchModel.PageNumber = pageIndex;
            this.getPageData();
        },
        //获取当前分页大小
        getPageSize(pageSize) {
            this.searchModel.PageSize = pageSize;
            this.getPageData();
        },
        //获取this.datas: {},//table数据对象
        getPageData() {
            var self = this;
            self.$http.post('Menu/PostPageList', this.searchModel).then(function (res) {
                if (res.data.status === 1) {
                    self.datas = res.data;
                }
                else {
                    self.$Modal.error({ title: '提示信息', content: res.data.message });
                }
            });
        },
        //编辑获取对象传入id
        editInfo(id) {//编辑
            var self = this;
            this.$http.get("Menu", { params: { id: id } }).then(//获取数据编辑项的值
                function (res) {
                    if (res.data.status == 1) {
                        if (res.data.model.ParentId == null) {
                            res.data.model.ParentId = '';
                        }
                        self.dataModel = res.data.model;
                        self.dataModel.Menus = self.dataModel.Menus.split(",");
                        self.showType = true;//打开modal
                    }
                    else {
                        self.$Modal.error({ title: "提示信息", content: "编辑数据加载失败!!!" });
                    }
                }
            );
        },
        //删除对象
        delInfo(id) {
            var self = this;
            self.$Modal.confirm({
                title: '提示信息',
                content: '<p>数据删除后不可恢复，确定要删除？</p>',
                onOk: () => {
                    this.$http.delete("Menu", { params: { id: id } }).then(
                        function (res) {
                            if (res.data.status == 1) {
                                self.$Notice.success({ title: '删除成功！', desc: false });
                                self.getPageData();
                                self.getParentList();
                            }
                            else {
                                self.$Modal.error({ title: "提示信息", content: "数据删除失败!!!" });
                            }
                        }
                    );
                }
            });
        },
        //保存方法
        save(modelname) {
            var self = this;
            this.$refs[modelname].validate((valid) => {
                if (valid) {
                    if (this.dataModel.Id === 0) {
                        this.$http.post('Menu', this.dataModel).then(
                            function (res) {
                                switch (res.data.status) {
                                    case 0:
                                        self.$Modal.info({ title: "提示信息", content: res.data.message, desc: false });
                                        break;
                                    case 1:
                                        self.getPageData();
                                        self.getParentList();
                                        self.restModel(modelname);//重置model
                                        self.showType = false;//关闭modal
                                        self.$Notice.success({ title: '保存成功！', desc: false });
                                        break;
                                    case -1:
                                        self.$Modal.warning({ title: "提示信息", content: "修改失败,请与管理员联系!" });
                                        break;
                                }
                            });
                    } else {
                        this.$http.put('Menu', this.dataModel).then(
                            function (res) {
                                switch (res.data.status) {
                                    case 0:
                                        self.$Modal.info({ title: "提示信息", content: res.data.message, desc: false });
                                        break;
                                    case 1:
                                        self.getPageData();
                                        self.getParentList();
                                        self.restModel(modelname);//重置model
                                        self.showType = false;//关闭modal
                                        self.$Notice.success({ title: '更新成功！', desc: false });
                                        break;
                                    case -1:
                                        self.$Modal.warning({ title: "提示信息", content: "修改失败,请与管理员联系!" });
                                        break;
                                }
                            });
                    }
                }
            });
        },
        //取消方法
        cancel(modelname) {
            console.log(1);
            this.restModel(modelname);
            this.showType = false;//关闭modal
            this.dataModel.Menus = [];
        },
        //重置model
        restModel(modelname) {
            this.$refs[modelname].resetFields();//重置model
            this.dataModel.MActions = [];
        },
        getDataIds(idList) {
            var ids = [];
            if (idList.length !== 0) {
                for (var i = 0; i < idList.length; i++) {
                    ids.push(idList[i].Id);
                }
            }
            return ids;
        },
        getParentList() {
            var self = this;
            self.ParentList = [];
            self.$http.get('Menu/GetParents').then(function (res) {
                if (res.data.status === 1) {
                    self.ParentList = res.data.model;
                }
                else {
                    self.$Modal.error({ title: '提示信息', content: res.data.message });
                }
            });
        },
        getParentName(ParentId) {
            var result = "";
            for (var i = 0; i < this.ParentList.length; i++) {
                if (this.ParentList[i].Id === ParentId) {
                    result = this.ParentList[i].MenuName;
                }
            }
            return result;
        }
    },
    //创建Vue页面方法
    created: function () {
        //创业Vue前调用获取this.datas: {},//table数据对象
        this.getPageData();
        this.getParentList();
        this.initMenu();
    },
    watch: {
        //需要监听的在此处新增监听对象
    }
}
</script>
